/*
 * door_control_dll_declarations.cpp
 *
 *  Created on: 19.12.2012
 *      Author: aska
 */

#include "door_control_dll_declarations.h"

door_control_lib::door_control_lib()
{
	return;
}
int door_control_lib::door_control_dll_load(void)
{
	int ret_val = 0;

	h_ser_port_dll = LoadLibrary("../../Door_control_dll/Release/Door_control_dll.dll");
	if (!(h_ser_port_dll > (void*)HINSTANCE_ERROR))
		h_ser_port_dll = LoadLibrary("Door_control_dll.dll");
	if (!(h_ser_port_dll > (void*)HINSTANCE_ERROR))
	{
		printf("unable LoadLibrary(""Door_control_dll.dll"")");
	}
	int load_func_err = 0;
	int func_counter = 0;
	if(h_ser_port_dll > (void*)HINSTANCE_ERROR)
	{
		func_counter++;
		if (! (door_control_init = (t_door_control_init)GetProcAddress(h_ser_port_dll, "door_control_init")))
		{
			printf("Error locating init function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_dev_if_mode = (t_door_control_dev_if_mode)GetProcAddress(h_ser_port_dll, "door_control_dev_if_mode")))
		{
			printf("Error locating door_control_dev_if_mode function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ser_set_name = (t_door_control_ser_set_name)GetProcAddress(h_ser_port_dll, "door_control_ser_set_name")))
		{
			printf("Error locating set_ser_name function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ser_set_num = (t_door_control_ser_set_num)GetProcAddress(h_ser_port_dll, "door_control_ser_set_num")))
		{
			printf("Error locating set_ser_name function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ser_set_baud = (t_door_control_ser_set_baud)GetProcAddress(h_ser_port_dll, "door_control_ser_set_baud")))
		{
			printf("Error locating set_ser_baud function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ser_get_baud = (t_door_control_ser_get_baud)GetProcAddress(h_ser_port_dll, "door_control_ser_get_baud")))
		{
			printf("Error locating get_ser_baud function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ser_read_buf = (t_door_control_ser_read_buf)GetProcAddress(h_ser_port_dll, "door_control_ser_read_buf")))
		{
			printf("Error locating get_ser_read_buf function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_get_brd_ver = (t_door_control_get_brd_ver)GetProcAddress(h_ser_port_dll, "door_control_get_brd_ver")))
		{
			printf("Error locating leds_set function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_set_brd_ver = (t_door_control_set_brd_ver)GetProcAddress(h_ser_port_dll, "door_control_set_brd_ver")))
		{
			printf("Error locating leds_set function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_set_ser_no = (t_door_control_set_ser_no)GetProcAddress(h_ser_port_dll, "door_control_set_ser_no")))
		{
			printf("Error locating leds_set function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_get_ser_no = (t_door_control_get_ser_no)GetProcAddress(h_ser_port_dll, "door_control_get_ser_no")))
		{
			printf("Error locating leds_set function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_get_fwv = (t_door_control_get_fwv)GetProcAddress(h_ser_port_dll, "door_control_get_fwv")))
		{
			printf("Error locating leds_set function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_leds_set = (t_door_control_leds_set)GetProcAddress(h_ser_port_dll, "door_control_leds_set")))
		{
			printf("Error locating leds_set function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		//if (! (ASSIGN_FUNC_FROM_DLL(h_ser_port_dll, door_control_lock_set)))
		if (! (door_control_lock_set = (t_door_control_lock_set)GetProcAddress(h_ser_port_dll, "door_control_lock_set")))
		{
			printf("Error locating open function \n");
			load_func_err |= 0x01 << func_counter;
		}
		func_counter++;
		if (! (door_control_inps_get = (t_door_control_inps_get)GetProcAddress(h_ser_port_dll, "door_control_inps_get")))
		{
			printf("Error locating open function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_modem_reset = (t_door_control_modem_reset)GetProcAddress(h_ser_port_dll, "door_control_modem_reset")))
		{
			printf("Error locating open function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ser_open = (t_door_control_ser_open)GetProcAddress(h_ser_port_dll, "door_control_ser_open")))
		{
			printf("Error locating open function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_dev_open = (t_door_control_dev_open)GetProcAddress(h_ser_port_dll, "door_control_dev_open")))
		{
			printf("Error locating open function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ft_get_driver_version = (t_door_control_ft_get_driver_version)GetProcAddress(h_ser_port_dll, "door_control_ft_get_driver_version")))
		{
			printf("Error locating door_control_ft_get_driver_version function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ft_get_library_version = (t_door_control_ft_get_library_version)GetProcAddress(h_ser_port_dll, "door_control_ft_get_library_version")))
		{
			printf("Error locating door_control_ft_get_library_version function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_ser_close = (t_door_control_ser_close)GetProcAddress(h_ser_port_dll, "door_control_ser_close")))
		{
			printf("Error locating close function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_d2xx_dll_open = (t_door_control_d2xx_dll_open)GetProcAddress(h_ser_port_dll, "door_control_d2xx_dll_open")))
		{
			printf("Error locating door_control_d2xx_dll_open function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (door_control_finish = (t_door_control_finish)GetProcAddress(h_ser_port_dll, "door_control_finish")))
		{
			printf("Error locating finish function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (send_ping = (t_send_ping)GetProcAddress(h_ser_port_dll, "send_ping")))
		{
			printf("Error locating send_ping function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (ping_start = (t_ping_start)GetProcAddress(h_ser_port_dll, "ping_start")))
		{
			printf("Error locating ping_start function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (ping_stop = (t_ping_stop)GetProcAddress(h_ser_port_dll, "ping_stop")))
		{
			printf("Error locating ping_stop function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		func_counter++;
		if (! (ping_addr_add = (t_ping_addr_add)GetProcAddress(h_ser_port_dll, "ping_addr_add")))
		{
			printf("Error locating ping_addr_add function \n");
			load_func_err |= 0x01 << func_counter;
		}
		else{}
		//FreeLibrary(h_ser_port_dll);
	}
	else
	{
		printf(" dll not loaded\n");
		load_func_err = 0xFFFF;
	}
	return ret_val;
}
int door_control_lib::dll_free(void)
{
	if(h_ser_port_dll > (void*)HINSTANCE_ERROR) FreeLibrary(h_ser_port_dll);
}

